package com.wxq.workpad.lailiao.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.QuickViewHolder;
import com.qianfanyun.qfui.rlayout.RTextView;
import com.wxq.workpad.R;
import com.wxq.workpad.entity.SaiXuanItem;
import com.wxq.workpad.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能详细描述
 *
 * @author xyx on 2017/8/15
 * @e-mail 384744573@qq.com
 * @see [相关类/方法](可选)
 */

public class ChooseSaiXuanDialogOld extends Dialog  {

    private Context mContext;
    private String apkInstallPath;
    private String updateUrl = "";
    private ClickItemListener listener;

    public ChooseSaiXuanDialogOld(Context context) {
        super(context, R.style.DialogTheme);
        mContext = context;
        View mContentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_saixuan, null);
        setContentView(mContentView);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);  //此处可以设置dialog显示的位置
        window.setLayout(DeviceUtils.dp2px(mContext, 310), ViewGroup.LayoutParams.WRAP_CONTENT); //设置宽高,这样设置才有效
        initView(mContentView);
    }

    RecyclerView recyclerView;
    TextView tv_confirm;
    TextView tv_cancel;
    BaseQuickAdapter adapter;
    private void initView(View mContentView) {
        tv_confirm=mContentView.findViewById(R.id.tv_confirm);
        tv_cancel=mContentView.findViewById(R.id.tv_cancel);

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<SaiXuanItem> selectList=new ArrayList<>();
                for (SaiXuanItem saiXuanItem : mlist) {
                    if (saiXuanItem.isSelect()) {
                        selectList.add(saiXuanItem);
                    }
                }

//                for (SaiXuanItem saiXuanItem : selectList) {
//                    saiXuanItem.setSelect(false);
//                }



                listener.getConfirmData(selectList);
                dismiss();

            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        recyclerView=mContentView.findViewById(R.id.recyvleview);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext,3));
        recyclerView.setAdapter(adapter=new BaseQuickAdapter<SaiXuanItem, QuickViewHolder>() {

            @NonNull
            @Override
            protected QuickViewHolder onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
                return new  QuickViewHolder(R.layout.item_saixuan, viewGroup);
            }

            @Override
            protected void onBindViewHolder(@NonNull QuickViewHolder quickViewHolder, int i, @Nullable SaiXuanItem saiXuanItem) {
                quickViewHolder.setText(R.id.tv_title_item,saiXuanItem.getName());
                quickViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        saiXuanItem.setSelect(!saiXuanItem.isSelect());
                        notifyDataSetChanged();
//                        Toast.makeText(mContext,"选中了"+saiXuanItem.getName(),Toast.LENGTH_SHORT).show();
                    }
                });
               RTextView rTextView= quickViewHolder.getView(R.id.tv_title_item);
                if (saiXuanItem.isSelect()) {
                    rTextView.getHelper().setBackgroundColorNormal(mContext.getResources().getColor(R.color.color_4474c3));
                    rTextView.getHelper().setBorderWidthNormal(DeviceUtils.dp2px(mContext,1.0f));
                    rTextView.setTextColor(mContext.getResources().getColor(R.color.TextColorWhite));
                }else {
                    rTextView.getHelper().setBackgroundColorNormal(mContext.getResources().getColor(R.color.color_f6f6f6));
                    rTextView.getHelper().setBorderWidthNormal(DeviceUtils.dp2px(mContext,0));
                    rTextView.setTextColor(mContext.getResources().getColor(R.color.color_898989));
                }
            }
        });
    }


    List<SaiXuanItem> mlist;

    public void showDialog(List<SaiXuanItem> list) {
        this.mlist=list;
        adapter.submitList(mlist);
        setCanceledOnTouchOutside(true);
        show();
    }


    public void setListener(ClickItemListener listener) {
        this.listener = listener;
    }

    public interface ClickItemListener {
        /**
         * app内下载Apk文件
         */
        void click(int position);
        void getConfirmData(  List<SaiXuanItem> data);

    }

    public void updateUI(){
        adapter.notifyDataSetChanged();
    }
}
